package com.jintoufs.service.hotel.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.hotel.HotelPriceMapper;
import com.jintoufs.dao.hotel.HotelRoomMapper;
import com.jintoufs.domain.constants.FullCalendar;
import com.jintoufs.domain.hotel.HotelPrice;
import com.jintoufs.domain.hotel.HotelRoom;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.hotel.HotelRoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelRoomServiceImpl implements HotelRoomService {


    @Resource
    private HotelRoomMapper hotelRoomMapper;

    @Resource
    private HotelPriceMapper hotelPriceMapper;

    @Override
    @SystemServiceLog(description = "删除房间")
    public void delete(String id) {
        String[] ids = id.split(",");
        for (String idm:ids){
            HotelRoom hotelRoom = hotelRoomMapper.selectByPrimaryKey(idm);
//            hotelPriceMapper.deleteByRoomId(idm);
            hotelRoom.setStatus("0");
//            hotelRoomMapper.deleteByPrimaryKey(idm);
            hotelRoomMapper.updateByPrimaryKeySelective(hotelRoom);
        }
    }

    @Override
    @SystemServiceLog(description = "修改房间信息")
    public void update(HotelRoom hotel) {
        hotelRoomMapper.updateByPrimaryKeySelective(hotel);
    }

    @Override
    @SystemServiceLog(description = "新增房间")
    public HotelRoom add(HotelRoom hotel) {
        hotelRoomMapper.insertSelective(hotel);
        return hotel;
    }

    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return hotelRoomMapper.queryByParams(params);
    }
    @Override
    public List<Map<String,Object>> queryPriceByParams(Map<String, Object> params) {
        return hotelRoomMapper.queryPriceByParams(params);
    }

    @Override
    public List<Map<String,Object>> selectByParams(Map<String, Object> params) {
        return hotelRoomMapper.selectByParams(params);
    }

    @Override
    public List<Map<String,Object>> getByFilter(QueryFilters filters) {
        return hotelRoomMapper.getByFilter(filters);
    }

    @Override
    public HotelRoom queryById(String id) {
        return hotelRoomMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<FullCalendar> getPriceCalendar(Map<String,Object> param) {
        return hotelPriceMapper.getPriceCalendar(param);
    }

    @Override
    public List<Map<String, Object>> getPriceCalendar2(Map<String, Object> param) {
        return hotelPriceMapper.getPriceCalendar2(param);
    }

    @Override
    public List<FullCalendar> getPriceCalendarForWeixin(Map<String,Object> param) {
        return hotelPriceMapper.getPriceCalendarForWeixin(param);
    }

    @Override
    public HotelPrice selectByHotelPrice(HotelPrice hotelPrice) {
        return hotelPriceMapper.selectByHotelPrice(hotelPrice);
    }

    @Override
    public void updatePrice(HotelPrice hotelPrice) {
        hotelPriceMapper.updatePrice(hotelPrice);
    }

    @Override
    public void savePrice(HotelPrice hotelPrice) {
        hotelPriceMapper.insert(hotelPrice);
    }

    @Override
    public void updateDate(String date) {
        hotelPriceMapper.updateDate(date);
    }

    @Override
    public HotelPrice updateReserve(HotelPrice hotelPrice) {
        hotelPriceMapper.updateReserve(hotelPrice);
        return hotelPrice;
    }

    @Override
    public int deleteByHotelId(String hotelId) {
        Map<String,Object> param = new HashMap<>();
        param.put("hotelId",hotelId);
        List<Map<String,Object>> roomList = hotelRoomMapper.selectByParams(param);
        for (Map<String,Object> room:roomList){
            delete(room.get("id").toString());
        }
        return 0;
    }

    @Override
    public HotelRoom selectById(String id) {
        return hotelRoomMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HotelPrice> getHotelPrice(Map<String, Object> param) {
        return hotelPriceMapper.selectByParam(param);
    }

    @Override
    public Integer getMinReserve(String id, String startDate, String endDate) {
        Map<String,Object> params = new HashMap<>();
        params.put("hotelRoomId",id);
        params.put("startDate",startDate);
        params.put("endDate",endDate);
        try {
            return hotelPriceMapper.getMinReserve(params);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }



}
